from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
from .tart_bar import TartBar
from collections import namedtuple

#
# widget for all the header plates 
#
class TartHeader_Item(QtGui.QWidget):
	def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)
#
#		make a label to hold the background image
#
		lab = QtGui.QLabel(self) 
		self.background = lab
#
#		set the tooltip color to something more in keeping
#		with the theme
#
		self.setStyleSheet("""
			QToolTip { background-color: darkorange; }
		""")
		self.col_name = "none"
		self.reverse = True

#
#	have a signal that means "sort using this column"
#
	sortme = QtCore.pyqtSignal(str, bool)

#
#	catch mouse clicks and emit sortme signals
#
	def mousePressEvent(self, e):
		self.reverse = not self.reverse
		self.sortme.emit(self.col_name, self.reverse)

#
#	make a property that sets the background
#
	def get_bg(self, path):
		return None
	def set_bg(self, path):
		self.background.setPixmap(QtGui.QPixmap(path))
	bg = property(get_bg, set_bg)

#
#	and another to set the tooltip
#
	def get_tt(self, path):
		return None
	def set_tt(self, tt):
		self.setToolTip(tt)
	tt = property(get_tt, set_tt)


class TartHeader(QtGui.QWidget):

#
#	have a signal that means "sort using this column"
#
	sortme = QtCore.pyqtSignal(str, bool)

	def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args)
		self.setGeometry(0, 0, 800, 24)

		xoff = 0
		yoff = 0
		l_name = TartHeader_Item(self)
		l_name.bg = "resources/tart_header_girls.png"
		l_name.tt = "The names of the girls working in this place"
		l_name.move(xoff,yoff)
		l_name.col_name = "name"
		l_name.sortme.connect(self.sortme)

		xoff += 235
		l_H = TartHeader_Item(self)
		l_H.bg = "resources/tart_header_H.png"
		l_H.tt = "Health: Green indicates a girl in good health."
		l_H.move(xoff,yoff)
		l_H.col_name = "health"
		l_H.sortme.connect(self.sortme)

		xoff += 42
		l_F = TartHeader_Item(self)
		l_F.bg = "resources/tart_header_F.png"
		l_F.tt = "Fatigue: Green means well rested and energetic"
		l_F.move(xoff,yoff)
		l_F.col_name = "fatigue"
		l_F.sortme.connect(self.sortme)

		xoff += 42
		l_M = TartHeader_Item(self)
		l_M.bg = "resources/tart_header_M.png"
		l_M.tt = "Morale: Green means happy and highly motivated"
		l_M.move(xoff,yoff)
		l_M.col_name = "morale"
		l_M.sortme.connect(self.sortme)

		xoff += 42
		l_morn = TartHeader_Item(self)
		l_morn.bg = "resources/tart_header_morning.png"
		l_morn.tt = "The Morning Shift: 7am to midday"
		l_morn.move(xoff,yoff)
		l_morn.col_name = "morning"
		l_morn.sortme.connect(self.sortme)

		xoff += 93
		l_after = TartHeader_Item(self)
		l_after.bg = "resources/tart_header_afternoon.png"
		l_after.tt = "The Afternoon Shift: from 1pm to 6pm"
		l_after.move(xoff,yoff)
		l_after.col_name = "afternoon"
		l_after.sortme.connect(self.sortme)

		xoff += 93
		l_eve = TartHeader_Item(self)
		l_eve.bg = "resources/tart_header_evening.png"
		l_eve.tt = "The Evening Shift: 7pm though to Midnight"
		l_eve.move(xoff,yoff)
		l_eve.col_name = "evening"
		l_eve.sortme.connect(self.sortme)

		xoff += 93
		l_night = TartHeader_Item(self)
		l_night.bg = "resources/tart_header_night.png"
		l_night.tt = "Night Time: The Graveyard Shift"
		l_night.move(xoff,yoff)
		l_night.col_name = "night"
		l_night.sortme.connect(self.sortme)


if __name__ == "__main__":
        import sys

        app = QtGui.QApplication(sys.argv)

        top = QtGui.QLabel()
        top.setGeometry(0, 0, 830, 150)
        path = "resources/wood.jpg"
        top.setPixmap(QtGui.QPixmap(path))

        head = TartHeader(top)
        head.move(45,45)

	Girly = namedtuple('Girly', 'name health fatigue morale jobs')
	bar2 = TartBar(top,
		index = 1,
		girl = Girly(
			name    = "Flossy Mossy",
			health  = 100,
			fatigue = 50,
			morale  = 10,
			jobs	= [
				"Assistant",
				"Assistant",
				"Assistant",
				"Assistant",
			]
		)
	)
        bar2.move(20,75)
        bar2.highlight()

        top.show()
        sys.exit(app.exec_())
